package main

import (
	"fmt"
	"sync"
	"time"
)

var mutex sync.Mutex
var ticket int = 10

func main() {
	go sale("售票口1")
	go sale("售票口2")
	go sale("售票口3")
	go sale("售票口4")

	time.Sleep(time.Second * 5)
}
func sale(name string) {
	for {
		mutex.Lock()
		if ticket > 0 {
			time.Sleep(time.Millisecond * 500)
			fmt.Println(name, "剩余票数", ticket)
			ticket--
		} else {
			mutex.Unlock()
			break
			fmt.Println("卖光了")
		}
		mutex.Unlock()
	}
}
